package com.example.yuejianapp14.Activity.Model;

import com.example.yuejianapp14.Activity.JavaBean.Comment;
import com.example.yuejianapp14.Activity.JavaBean.Success;
import com.example.yuejianapp14.Activity.JavaBean.Tiezi;
import com.example.yuejianapp14.Activity.Listener.ClubNoListListener;
import com.example.yuejianapp14.Activity.Listener.CommentListener;
import com.example.yuejianapp14.Activity.Listener.ListListener;
import com.example.yuejianapp14.Activity.Listener.TieziListener;
import com.example.yuejianapp14.Activity.Servics.CommentService;
import com.example.yuejianapp14.Activity.Servics.TieziService;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by jason_lin on 2017/10/19.
 */

public class TieziModel {
    private Retrofit retrofit;
    private String BASEURL = "http://10.0.2.2/YueJian/index.php/Api/Tiezi/";
    private String HeadPhotoNameDefault = "user_headphoto.png";

    public TieziModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public void getTieziList(final TieziListener listener){
        TieziService clubServic=retrofit.create(TieziService.class);
        Call<List<Tiezi>> call=clubServic.getTieze();
        call.enqueue(new Callback<List<Tiezi>>() {
            @Override
            public void onResponse(Call<List<Tiezi>> call, Response<List<Tiezi>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<List<Tiezi>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    public void TieziInfo (String tid, final ClubNoListListener listener){
        TieziService tieziServic=retrofit.create(TieziService.class);
        Call call = tieziServic.tieziinfo(tid);
        Callback<Tiezi> callback =new Callback<Tiezi>() {
            @Override
            public void onResponse(Call<Tiezi> call, Response<Tiezi> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("onresponse fail");

            }

            @Override
            public void onFailure(Call<Tiezi> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
    public void getComment(String tid, final CommentListener listener){
        CommentService commentService=retrofit.create(CommentService.class);
        Call<List<Comment>> call=commentService.commentlist(tid);
        call.enqueue(new Callback<List<Comment>>() {
            @Override
            public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<List<Comment>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    public void AddComment(String uid,String tid , String content ,final ListListener<Success> listener){
        CommentService commentService=retrofit.create(CommentService.class);
        Call<Success> call=commentService.addcomment( uid,tid,content);
        call.enqueue(new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<Success> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }


    public void findTiezi(String uid, final TieziListener listener){
        TieziService clubServic=retrofit.create(TieziService.class);
        Call<List<Tiezi>> call=clubServic.findTiezi(uid);
        call.enqueue(new Callback<List<Tiezi>>() {
            @Override
            public void onResponse(Call<List<Tiezi>> call, Response<List<Tiezi>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<List<Tiezi>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    public void Releasetiezi(String uid, String content, String path,final ListListener<Success> listener){
        TieziService tieziService=retrofit.create(TieziService.class);
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(), requestFile);
        Call<Success> call=tieziService.Releasetiezi( uid,content,body);
        call.enqueue(new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {

                listener.onResponse(response.body());
//                if (response.isSuccessful() && response.body() != null) {
//                    listener.onResponse(response.body());
//                } else
//                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<Success> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
